package com.jieao.content.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jieao.common.constant.ContentConstants;
import com.jieao.common.core.domain.Ztree;
import com.jieao.common.exception.BusinessException;
import com.jieao.common.utils.StringUtils;
import com.jieao.content.domain.Catalog;
import com.jieao.content.dto.CatalogDto;
import com.jieao.content.mapper.CatalogMapper;
import com.jieao.content.service.ICatalogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author weifanl
 * @Date2020/5/21 15:33
 **/
@Service
public class CatalogServiceImpl extends ServiceImpl<CatalogMapper, Catalog> implements ICatalogService {

    @Override
    public List<CatalogDto> selectCatalogList(CatalogDto catalog) {
        return baseMapper.selectCatalogList(catalog);
    }

    /**
     * 对象转目录树
     *
     * @param catalogList 目录列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<CatalogDto> catalogList) {

        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (CatalogDto catalog : catalogList) {
            if (ContentConstants.CATALOG_NORMAL.equals(catalog.getStatus())) {
                Ztree ztree = new Ztree();
                ztree.setId(catalog.getId());
                ztree.setpId(catalog.getParentId());
                ztree.setName(catalog.getCatalogName());
                ztree.setTitle(catalog.getCatalogName());
                ztrees.add(ztree);
            }
        }
        return ztrees;
    }

    /**
     * 删除目录管理信息
     *
     * @param catalogId 目录ID
     * @return 结果
     */
    @Override
    public int deleteCatalogById(Long catalogId) {
        return baseMapper.deleteById(catalogId);
    }

    /**
     * 新增保存目录信息
     *
     * @param catalog 目录信息
     * @return 结果
     */
    @Override
    public int insertCatalog(Catalog catalog) {
        Catalog info = baseMapper.selectById(catalog.getParentId());
        // 如果父节点不为"正常"状态,则不允许新增子节点
        if (!ContentConstants.CATALOG_NORMAL.equals(info.getStatus())) {
            throw new BusinessException("目录停用，不允许新增");
        }
        catalog.setAncestors(info.getAncestors() + "," + catalog.getParentId());
        return baseMapper.insert(catalog);
    }

    /**
     * 修改保存目录信息
     *
     * @param catalog 目录信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCatalog(Catalog catalog) {
        Catalog newParentCatalog = baseMapper.selectById(catalog.getParentId());
        Catalog oldCatalog = selectCatalogById(catalog.getId());
        if (StringUtils.isNotNull(newParentCatalog) && StringUtils.isNotNull(oldCatalog)) {
            String newAncestors = newParentCatalog.getAncestors() + "," + newParentCatalog.getId();
            String oldAncestors = oldCatalog.getAncestors();
            catalog.setAncestors(newAncestors);
            updateCatalogChildren(catalog.getId(), newAncestors, oldAncestors);
        }
        int result = baseMapper.updateById(catalog);
        if (ContentConstants.CATALOG_NORMAL.equals(catalog.getStatus())) {
            // 如果该目录是启用状态，则启用该目录的所有上级目录
            updateParentCatalogStatus(catalog);
        }
        return result;
    }

    /**
     * 修改该目录的父级目录状态
     *
     * @param catalog 当前目录
     */
    private void updateParentCatalogStatus(Catalog catalog) {
        String updateBy = catalog.getUpdateBy();
        catalog = baseMapper.selectById(catalog.getId());
        catalog.setUpdateBy(updateBy);
        baseMapper.updateById(catalog);
    }

    /**
     * 修改子元素关系
     *
     * @param catalogId    被修改的目录ID
     * @param newAncestors 新的父ID集合
     * @param oldAncestors 旧的父ID集合
     */
    public void updateCatalogChildren(Long catalogId, String newAncestors, String oldAncestors) {
        List<CatalogDto> children = baseMapper.selectChildrenCatalogById(catalogId);
        for (CatalogDto child : children) {
            child.setAncestors(child.getAncestors().replace(oldAncestors, newAncestors));
        }
        if (children.size() > 0) {
            baseMapper.updateCatalogChildren(children);
        }
    }

    /**
     * 根据目录ID查询信息
     *
     * @param catalogId 目录ID
     * @return 目录信息
     */
    @Override
    public CatalogDto selectCatalogById(Long catalogId) {
        return baseMapper.selectCatalogById(catalogId);
    }

    /**
     * 查询目录管理树
     *
     * @param catalog 目录信息
     * @return 所有目录信息
     */
    @Override
    public List<Ztree> selectCatalogTree(CatalogDto catalog) {
        List<CatalogDto> catalogList = baseMapper.selectCatalogList(catalog);
        List<Ztree> ztrees = initZtree(catalogList);
        return ztrees;
    }

    @Override
    public List<Ztree> selectCatalogTree() {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        List<CatalogDto> catalogList = selectCatalogList(new CatalogDto());
        ztrees = initZtree(catalogList);
        return ztrees;
    }

    @Override
    public int selectCatalogCount(Long catalogId) {
        return baseMapper.selectCatalogCount(catalogId);
    }


}
